import * as types from "./action-types"
import axios from "../../../utils/axios"
import { fromJS } from "immutable"

export const handleSearchFocus = () => ({ type: types.CLICK_SEARCH_FOCUS })
export const handleSearchOnblur = () => ({ type: types.CLICK_SEARCH_ONBLUR })
export const handleMoveIn = () => ({ type: types.MOVE_SEARCH_IN })
export const handleMoveOut = () => ({ type: types.MOVE_SEARCH_OUT })
export const changePages = () => ({ type: types.CHANGE_SEARCH_PAGES })

const getSearchInfo = (data) => ({ type: types.GET_SEARCH_INFO, data: fromJS(data), totalPages: Math.ceil(data.length / 10) })

export const getSearchInfoAsyn = () => {
    return (dispatch) => {
        axios.get("http://localhost:3000/api/searchInfo.json").then(res => {
            const data = res.data;
            dispatch(getSearchInfo(data))
        })

    }
}

export const findUser = () => {
    return (dispatch) => {
        axios.get("/user/test").then(res => {
            console.log(res);
        })
    }
}

export const getCourses = () => {
    return (dispatch) => {
        axios.get("/courses/findAllCourses?skip=0&limit=30").then(res => {
            console.log(res);
        })
    }
}